package md.lanjing.com.module_mine.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.CountDownTimerUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

import com.lxh.baselibray.constant.Constant;

import md.lanjing.com.module_mine.R;
import md.lanjing.com.module_mine.R2;
import md.lanjing.com.module_mine.bean.ModifyDealPwdSecurityCodeResponse;
import md.lanjing.com.module_mine.contact.ModifyDealPwdContact;
import md.lanjing.com.module_mine.bean.ModifyDealPwdResponse;
import retrofit2.Response;

/**
 * 修改交易密码
 */
public class ModifyDealPwdActivity extends MvpActivity<ModifyDealPwdContact.ModifyDealPwdPresent> implements ModifyDealPwdContact.IModifyDealPwdView {
    @BindView(R2.id.et_tel)
    EditText etTel;
    @BindView(R2.id.et_security_code)
    EditText etSecurityCode;
    @BindView(R2.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R2.id.et_dealPwd)
    EditText etDealPwd;
    @BindView(R2.id.et_confirm_deal_pwd)
    EditText etConfirmDealPwd;




    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_deal_pwd;
    }



    @OnClick({R2.id.tv_get_code, R2.id.btn_confirm_modify})
    public void onViewClicked(View view) {
        String phone=etTel.getText().toString();
        int i = view.getId();
        if (i == R.id.tv_get_code) {
            if (ObjectUtils.isEmpty(phone)) {
                ToastUtils.showLongToast(context, "手机号不能为空");
                return;
            }
            mPresent.getModifyDealPwdCode(context, phone);

        } else if (i == R.id.btn_confirm_modify) {
            String code = etSecurityCode.getText().toString();
            String dealPwd = etDealPwd.getText().toString();
            String confirmDealPwd = etConfirmDealPwd.getText().toString();
            if (ObjectUtils.isEmpty(phone)) {
                ToastUtils.showLongToast(context, "手机号不能为空");
                return;
            }
            if (ObjectUtils.isEmpty(code)) {
                ToastUtils.showLongToast(context, "验证码不能为空");
                return;
            }
            if (ObjectUtils.isEmpty(dealPwd)) {
                ToastUtils.showLongToast(context, "交易密码不能为空");
                return;
            }
            if (ObjectUtils.isEmpty(confirmDealPwd)) {
                ToastUtils.showLongToast(context, "确认交易密码不能为空");
                return;
            }
            if (!ObjectUtils.equals(dealPwd, confirmDealPwd)) {
                ToastUtils.showLongToast(context, "两次交易密码不一致");
                return;
            }
            showLoadingDialog();

            mPresent.modifyDealPwd(context, code, dealPwd, phone);

        }
    }

    @Override
    public void getModifyDealPwdCodeData(Response<ModifyDealPwdSecurityCodeResponse> response) {
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            ToastUtils.showLongToast(context,"验证码已发送");
            CountDownTimerUtils countDownTimerUtils=new CountDownTimerUtils(tvGetCode,60000,1000);
            countDownTimerUtils.start();
        }
    }

    @Override
    public void getModifyDealPwdData(Response<ModifyDealPwdResponse> response) {
        dismissLoadingDialog();
        if (response.body().getCode()==Constant.SUCCESS_CODE){
            ToastUtils.showLongToast(context,"修改交易密码成功");
            finish();
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected ModifyDealPwdContact.ModifyDealPwdPresent createPresent() {
        return new ModifyDealPwdContact.ModifyDealPwdPresent();
    }
}
